package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingProjectServiceImpl
 * @Author Wen Jie
 * @Date 2024-09-05
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> nursingProjects = nursingProjectMapper.getByPage(name, status);
        PageResponse<NursingProjectVo> projectVoPageResponse = PageResponse.of(nursingProjects, NursingProjectVo.class);
        return projectVoPageResponse;
    }

    @Override
    public void addNursingProject(NursingProjectVo nursingProjectVo) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectVo, NursingProject.class);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        NursingProjectVo vo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return vo;
    }


    @Override
    public void updateNursingProject(NursingProjectVo nursingProjectVo) {
        Long id = nursingProjectVo.getId();
        int i = countNursingProject(id);
        if (i > 0){
            throw new RuntimeException("当前项目被引用，无法修改");
        }
        if (nursingProjectVo.getStatus() == 0){
            throw new RuntimeException("当前项目已禁用，无法修改");
        }
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectVo, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(nursingProjectVo.getUpdateBy());
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void deleteById(Long id) {
        int i = countNursingProject(id);
        if (i > 0){
            throw new RuntimeException("当前项目被引用，无法修改");
        }
        int status = getStatusById(id);
        if (status == 1){
            throw new RuntimeException("当前项目已启用，无法删除");
        }
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProject> nursingProjects = nursingProjectMapper.getAll();
        List<NursingProjectVo> nursingProjectVos = BeanUtil.copyToList(nursingProjects, NursingProjectVo.class);
        return nursingProjectVos;
    }

    @Override
    public void updateStatusById(Long id, Integer status) {
        int i = countNursingProject(id);
        if (i > 0){
            throw new RuntimeException("当前项目被引用，无法修改");
        }
        nursingProjectMapper.updateStatusById(id, status);
    }

    public int countNursingProject(Long id) {
        int count = nursingProjectMapper.countById(id);
        return count;
    }

    public int getStatusById(Long id) {
        int status = nursingProjectMapper.getStatusById(id);
        return status;
    }
}
